package com.cction.comm;

import com.cction.entry.TaxDetailBean;

import java.util.ArrayList;

/**
 * 个税计算
 */
public class TaxCCtionUtils {

    public static double base = 5000.00;// 纳税基准
    static double rateAmount = 0;// 每月税额
    static double beforeAmount = 0;// 税前金额
    static double totlerate = 0;// 累计税额
    static double[] rate;// 税率
    static double p = 0;// 专项扣除
    static double q = 0;// 速算扣除
    static double n = 0;// 五险一金扣除

    static int month = 1;// 月份
    static double[][] rates = {{0, 0, 0},
            {36000, 0.03, 0},
            {144000, 0.1, 2520},
            {300000, 0.2, 16920},
            {420000, 0.25, 31920},
            {660000, 0.3, 52920},
            {960000, 0.35, 85920},
            {999999999, 0.45, 181920}};

    public static double[] getRate(double totale) {
        double[] rate = {36000, 0.03, 0};
        for (int i = 1; i < rates.length; i++) {
            if (totale <= rates[0][0]) {
                rate = rates[0];
            } else if (totale > rates[i - 1][0] && totale <= rates[i][0]) {
                rate = rates[i];
                break;
            }

        }

        return rate;
    }

    /**
     * 每个月税收记录
     *
     * @param beforeAmount 每月税前金额
     * @param n            五险一金扣除金额
     * @param q            专项扣除金额
     * @return
     */
    public static ArrayList<TaxDetailBean> getTaxs(double beforeAmount, double mimnum, double n, double q) {
        ArrayList<TaxDetailBean> arrayList = new ArrayList<>();
        rateAmount = 0;
        totlerate = 0;
        //月份
        month = 12;
        // 纳税基准
        base = mimnum;

        for (int i = 1; i <= month; i++) {
            double amount = (beforeAmount - n - q - base) * i;
            rate = getRate(amount);
            rateAmount = Math.round((amount * rate[1]) * 100) / 100 - rate[2] - totlerate;
            System.out.println("第" + i
                    + "月:应纳税金额：" + amount
                    + ",税率：" + rate[1]
                    + ",速算扣除金额：" + rate[2]
                    + ",往月纳税金额合计：" + totlerate
                    + ",本月应缴税额：" + rateAmount);
            TaxDetailBean taxDetailBean = new TaxDetailBean();
            taxDetailBean.position = i;
            taxDetailBean.tax = rateAmount;
            taxDetailBean.result = beforeAmount - rateAmount - n - q;
            arrayList.add(taxDetailBean);
            totlerate = totlerate + rateAmount;
        }
        return arrayList;
    }


    public static void main(String[] args) {

        rateAmount = 0;
        totlerate = 0;
        System.out.println("-----------个税计算-------------");
        //每月税前金额
        beforeAmount = 18000;
        //五险一金扣除金额
        n = 2400;
        //专项扣除金额
        q = 0;
        //月份
        month = 12;
        for (int i = 1; i <= month; i++) {
            double amount = (beforeAmount - n - q - base) * i;
            rate = getRate(amount);
            rateAmount = Math.round((amount * rate[1]) * 100) / 100 - rate[2] - totlerate;
            System.out.println("第" + i
                    + "月:应纳税金额：" + amount
                    + ",税率：" + rate[1]
                    + ",速算扣除金额：" + rate[2]
                    + ",往月纳税金额合计：" + totlerate
                    + ",本月应缴税额：" + rateAmount);
            totlerate = totlerate + rateAmount;
        }
    }

}
